import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxLength: 300,
    minLength: 10,
    minWord: '',
    currentWordNumber: '',
    goodsObj: {},
    userAddress: {},
    index: ''
  },
  //设置一个定时器时间
  TimeId: -1,
  //请求参数
  QueryParams: {
    goodsName: "",
    goodsClassify: "",
    orderType: "",
    pageNum: 1,
    pageSize: 10
  },
  header: {},
  //社区 团长 信息
  communityObj: {},
  //提交订单的请求参数
  orderParams: {
    goodsId: '',
    groupLeader: '',
    address: '',
    orderRemark: ''
  },
  //获取textarea输入的字符的长度
  getValueLength(e) {
    let { value } = e.detail
    this.orderParams.orderRemark = value
    let len = parseInt(value.trim().length)
    if (len < this.data.minLength) {
      this.setData({
        minWord: '最少输入十个字哦~'
      })
    } else {
      this.setData({
        minWord: ''
      })
    }
    clearTimeout(this.TimeId)
    this.TimeId = setTimeout(() => {
      this.setData({
        currentWordNumber: len
      })
      if (len === 0) {
        this.setData({
          minWord: ''
        })
      }
    }, 500)
    if (len > this.data.maxLength) {
      this.setData({
        currentWordNumber: len
      })
    }
    this.setData({
      textVal: e.detail.value
    })
  },
  //提交报修订单
  async submitOrder() {
    // wx.navigateTo({
    //   url: "../repair-pay/index"
    // });
    const header = this.header
    this.orderParams.goodsId = this.QueryParams.goodsId
    this.orderParams.groupLeader = this.communityObj.groupLeader
    this.orderParams.address = this.data.userAddress.addressId
    const res = await request({ url: '/order/maintenance', data: this.orderParams, method: 'POST', header })
    const { orderId } = res.data
    if (res.data.payment) {
      wx.navigateTo({
        url: '../repair-pay/index?orderId=' + orderId + "&goodsId=" + this.QueryParams.goodsId
      });
    } else {
      wx.navigateTo({
        url: '../maintenance-order/index?type=3'
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { goodsId, goodsClassify } = options
    this.QueryParams.goodsId = goodsId
    // this.QueryParams.goodsClassify = goodsClassify
    const token = wx.getStorageSync("token");
    const communityObj = wx.getStorageSync("communityObj")
    this.communityObj = communityObj
    this.header = { token }
    this.getGoodsDetail()
  },
  //获取商品详细信息
  async getGoodsDetail() {
    const goodsId = this.QueryParams.goodsId
    const header = this.header
    const res = await request({ url: '/maintenance/goods/' + goodsId + '/showList', header })
    this.setData({
      goodsObj: res.data
    })
    console.log(res)
  },
  //获取用户收货地址
  async getUserAddress() {
    const header = this.header
    const res = await request({ url: '/user/address/personalList', header })
    const { index } = this.data
    if (index !== '') {
      this.setData({
        userAddress: res.data[index]
      })
    } else if (res.data.length >= 0) {
      const index = res.data.findIndex(v => v.addressDefault === true)
      if (index !== -1) {
        this.setData({
          userAddress: res.data[index]
        })
      } else {
        this.setData({
          userAddress: res.data[0]
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})